<?php

namespace app\store\model;


/**
 * 订单管理
 * Class Order
 * @package app\store\controller
 */
class Orders extends \app\common\model\Orders
{

    /**
     * 订单列表
     * @param string $dataType
     * @param array $query
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
   public function getList($dataType, $query = [])
   {
       // 检索查询条件
       !empty($query) && $this->setWhere($query);
       $list = $this->alias('orders')
           ->join('user', 'user.user_id = orders.user_id')
           ->where($this->transferDataType($dataType))
           ->where('orders.is_delete', '=', 0)
           ->order(['orders.create_time' => 'desc'])
           ->join('OrderTicket', 'OrderTicket.order_id = orders.ticket_ids')
           ->join('scenic', 'scenic.id = OrderTicket.scenic_id')
           ->join('UploadFile', 'UploadFile.file_id = scenic.logo_image_id')
           ->paginate(10, false, [
               'query' => \request()->request()
           ]);
       return $list;
   }

   private function setWhere($query)
   {
       if (isset($query['start_time']) && !empty($query['start_time'])) {
           $this->where('orders.start_time', '>=', strtotime($query['start_time']));
       }
       if (isset($query['end_time']) && !empty($query['end_time'])) {
           $this->where('orders.end_time', '<', strtotime($query['end_time']) + 86400);
       }
       // 用户id
       if (isset($query['user_id']) && $query['user_id'] > 0) {
           $this->where('orders.user_id', '=', (int)$query['user_id']);
       }
   }

   private function transferDataType($dataType)
   {
       // 数据类型
       $filter = [];
       switch ($dataType) {
           case 'all':
               $filter = [];
               break;
           case 'pay':
               $filter = ['pay_status' => 1];
               break;
           case 'inspect':
               $filter = ['order_status' => 20];
               break;
           case 'confirm':
               $filter = ['order_status' => 30];
               break;
           case 'complete':
               $filter = ['order_status' => 50];
               break;
       }
       return $filter;
   }

}